package cn.itcast.service.impl;

import cn.itcast.dao.RoleDao;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Menu;
import cn.itcast.pojo.Permission;
import cn.itcast.pojo.Role;
import cn.itcast.service.RoleService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import cn.itcast.constant.MessageConstant;
import cn.itcast.dao.RoleDao;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.pojo.CheckItem;
import cn.itcast.pojo.Role;
import cn.itcast.service.RoleService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

import java.util.List;


/**
 * @author Administrator
 */
@Service
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;


    //新增
    @Override
    public void add(Role role, String[] permissionIds, String[] menuIds) {
       //新增角色信息
        roleDao.add(role);
       /* //新增角色关联菜单
        roleDao.addRoleMenu(role.getId(), menuIds);
        //新增角色关联权限
        roleDao.addRolePermissionIds(role.getId(), permissionIds);*/
    }


    //分页查询
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        //开启分页查询
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //根据条件分页查询检查项信息
        Page<Role> page = roleDao.findByPage(queryPageBean.getQueryString());
        //构建分页数据返回
        return new PageResult(page.getTotal(), page.getResult());
    }


    //删除
    @Override
    public void deleteById(Integer id) {
        //判断该检查项是否被引用
        int count = roleDao.selectRoleReMenuCount(id);
        int count1 = roleDao.selectRolePermissionCount(id);
        if (count > 0 || count1 > 0) {
            throw new RuntimeException("当前角色被关联，不能删除 !");
        }
        roleDao.deleteById(id);
    }


    //修改
    @Override
    public void edit(Role role) {
        roleDao.update(role);
    }


    //根据id查询角色
    @Override
    public Role findById(int id) {
        return roleDao.findById(id);
    }




    @Override
    public String[] findByMenuId(Integer id) {
        return roleDao.findByMenuId(id);

    }

    @Override
    public String[] findByIdPermission(Integer id) {
        return roleDao.findByIdPermission(id);
    }


}